package com.ruoyi.project.mahjong.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.service.IShopLogService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.SendMoney;
import com.ruoyi.project.mahjong.service.ISendMoneyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 取款记录Controller
 *
 * @author ruoyi
 * @date 2023-12-01
 */
@RestController
@RequestMapping("/mahjong/sendMoney")
public class SendMoneyController extends BaseController {
    @Autowired
    private ISendMoneyService sendMoneyService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IShopLogService shopLogService;
    /**
     * 查询取款记录列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:list')")
    @GetMapping("/list")
    public TableDataInfo list(SendMoney sendMoney) {
        startPage();
        List<SendMoney> list = sendMoneyService.selectSendMoneyList(sendMoney);
        return getDataTable(list);
    }

    /**
     * 取钱
     * @param money
     * @return
     */
    @GetMapping("/savemoney/{money}")
    public AjaxResult savemoney(@PathVariable String money) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        Long jbid = redisCache.getCacheObject(shopId + user.getUserId().toString());
        SendMoney sendMoney = new SendMoney();
        sendMoney.setMoney(money);
        sendMoney.setSendTime(new Date());
        sendMoney.setShopId(shopId);
        sendMoney.setJbId(jbid);
        shopLogService.saveSendMoney(sendMoney);
        return AjaxResult.success(sendMoneyService.insertSendMoney(sendMoney));
    }

    /**
     * 导出取款记录列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:export')")
    @Log(title = "取款记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SendMoney sendMoney) {
        List<SendMoney> list = sendMoneyService.selectSendMoneyList(sendMoney);
        ExcelUtil<SendMoney> util = new ExcelUtil<SendMoney>(SendMoney.class);
        return util.exportExcel(list, "sendMoney");
    }

    /**
     * 获取取款记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sendMoneyService.selectSendMoneyById(id));
    }

    /**
     * 新增取款记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:add')")
    @Log(title = "取款记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SendMoney sendMoney) {
        return toAjax(sendMoneyService.insertSendMoney(sendMoney));
    }

    /**
     * 修改取款记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:edit')")
    @Log(title = "取款记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SendMoney sendMoney) {
        return toAjax(sendMoneyService.updateSendMoney(sendMoney));
    }

    /**
     * 删除取款记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:sendMoney:remove')")
    @Log(title = "取款记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sendMoneyService.deleteSendMoneyByIds(ids));
    }
}
